package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.util.JSONUtils;
import com.common.util.LogUtil;
import com.dmooo.yxmmerchant.domain.MallGoodsBean;
import com.dmooo.yxmmerchant.domain.MallGoodsDetailBean;
import com.dmooo.yxmmerchant.view.contract.MallGoodsContract;
import com.dmooo.yxmmerchant.view.model.MallGoodsModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class MallGoodsPresenter extends BasePresenter<MallGoodsContract.MallGoodsView> implements MallGoodsContract.MallGoodsPtr {
    private MallGoodsModel model;
    GsonBuilder builder=new GsonBuilder();
    public MallGoodsPresenter(MallGoodsContract.MallGoodsView mView, Context context) {
        super(mView);
        model=new MallGoodsModel(context);
        builder.serializeNulls();
    }

    @Override
    public void getMallGoods(Map<String, Object> map) {
        LogUtil.d("dsfasdf",map.toString());
        model.getMallGoods(map, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("dsafds",o.toString());
                try {
                    JSONObject obj=new JSONObject(builder.create().toJson(o));
                    mView.showMallGoods(new Gson().fromJson(obj.toString(),MallGoodsBean.class).list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getGoodsDetail(String goods_id) {
        model.getGoodsDetail(goods_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                LogUtil.d("dsafds",o.toString());
                try {
                    JSONObject object=new JSONObject(builder.create().toJson(o));
                    mView.showGoodsDetail(new Gson().fromJson(object.toString(),MallGoodsDetailBean.class));
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }

            }

            @Override
            public void onFail(Object o) {
//                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void addShopCar(String token, String goods_id, Map<String, Object> map) {
        model.addShopCar(token, goods_id, map, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.addShopCarSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void collectGoods(String token, String goods_id) {
        model.collectGoods(token, goods_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.collectGoodsSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void disCollectGoods(String token, String goods_id) {
        model.disCollectGoods(token, goods_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.disCollectGoodsSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void isCollectGoods(String token, String goods_id) {
        model.isCollectGoods(token, goods_id, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.isCollectGoodsSuccess("Y".equals(JSONUtils.getAsJsonObject(o).get("is_collect").getAsString()));
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
